<template>
  <div class='container'>
    <el-card class="box-card">
      <el-form :model="searchParams" label-width="80px" @submit.native.prevent="">
        <el-row>
          <el-col :span="5">
            <el-form-item
              prop="directoryName"
              label="目录名称">
              <el-input v-model="searchParams.directoryName"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              prop="state"
              label="状态">
              <el-select v-model="searchParams.state" placeholder="请选择" style="margin-right: 10px">
                <el-option
                  v-for="item in statusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-button @click.prevent="clearStatus">清除</el-button>
              <el-button type="primary" @click.prevent="doSearch">搜索</el-button>
            </el-form-item>
          </el-col>
          <el-form-item style="float: right">
            <el-button @click.prevent="dialogShow = true" type="success" icon="el-icon-edit">新增目录
            </el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <el-alert
        :closable="false"
        type="info">
        <i class="el-icon-info" style="font-size: 14px">&emsp;&emsp;数据一共 {{ totalNum }} 条</i>
      </el-alert>
      <el-table
        :data="list"
        stripe
        style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          width="50">
        </el-table-column>
        <el-table-column
          prop="subjectName"
          label="所属学科"
        >
        </el-table-column>
        <el-table-column
          prop="directoryName"
          label="目录名称"
        >
        </el-table-column>
        <el-table-column
          prop="username"
          label="创建者"
        >
        </el-table-column>
        <el-table-column
          prop="addDate"
          label="创建日期"
        >
          <template v-slot="scope">
            <i>{{ scope.addDate|getButtyTime }}</i>
          </template>
        </el-table-column>
        <el-table-column
          prop="totals"
          label="面试题数量"
        >
        </el-table-column>

        <el-table-column
          label="状态">
          <template v-slot='scope'>
            <span v-if="scope.row.state===1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          width="200">
          <template v-slot="scope">
            <div>
              <el-button
                type="text"
                @click="toggleState(scope.row)">
                {{ scope.row.state === 1 ? '禁用' : '启用' }}
              </el-button>
              <el-button
                :disabled="scope.row.state !== 1"
                type="text"
                @click="onEditClick(scope.row)">
                修改
              </el-button>
              <el-button
                :disabled="scope.row.state !== 1"
                type="text"
                @click="deleteDirectory(scope.row)">
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalNum"
        :page-size="searchParams.pagesize"
        @current-change="onPageChange"
        style="margin-top: 10px">
      </el-pagination>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="dialogShow"
      width="30%"
      @close="onDialogClose">
      <el-form ref="submitDialog" :model="submitData" label-width="80px" @submit.native.prevent="">
        <el-form-item
          prop="subjectID"
          label="所属学科"
          :rules="[{required:true,message: '所属学科不能为空',trigger: 'blur'}]">
          <el-select v-model="submitData.subjectID" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in subjectList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="directoryName"
          label="目录名称"
          :rules="[{required:true,message: '目录名不能为空',trigger: 'blur'}]">
          <el-input v-model="submitData.directoryName"/>
        </el-form-item>
      </el-form>
      <template v-slot:footer class="dialog-footer">
        <el-button @click="dialogShow=false">取 消</el-button>
        <el-button type="primary" @click="submitOk">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { list as directoryList, changeState, remove, update, add } from '@/api/hmmm/directorys'
import { status as statusList } from '@/api/hmmm/constants'
import moment from 'moment'
import { simple } from '@/api/hmmm/subjects'

export default {
  name: 'directorys',
  data () {
    return {
      dialogShow: false,
      totalNum: 0,
      list: [],
      subjectList: [],
      searchParams: {
        directoryName: null,
        state: null,
        page: 1,
        pagesize: 5
      },
      submitData: {
        directoryName: '',
        id: '',
        subjectID: ''
      }
    }
  },
  methods: {
    onPageChange (page) {
      console.log(page)
      this.searchParams.page = page
      this.getDirectList()
    },
    async getDirectList () {
      const res = await directoryList(this.searchParams)
      this.totalNum = res.data.counts
      this.list = res.data.items
    },
    clearStatus () {
      this.searchParams.state = null
      this.doSearch()
    },
    doSearch () {
      this.getDirectList()
    },
    async toggleState (item) {
      const cState = item.state === 0 ? 1 : 0
      const res = await changeState({
        id: item.id,
        state: cState
      })
      if (res.data.success) {
        item.state = cState
      }
    },
    async deleteDirectory (item) {
      await remove({ id: item.id })
      const index = this.list.findIndex(obj => {
        return obj === item
      })
      if (index) {
        this.list.splice(index, 1)
      }
    },
    async getSimpleSubjects () {
      try {
        const res = await simple()
        this.subjectList = res.data
      } catch (e) {
      }
    },
    submitOk () {
      try {
        this.$refs.submitDialog.validate(async isPass => {
          if (isPass) {
            if (this.submitData.id) {
              // 修改
              const res = await update(this.submitData)
              console.log(res)
            } else {
              // 新增
              const res = await add(this.submitData)
              console.log(res)
            }
            this.onDialogClose()
          }
        })
      } catch (e) {
        console.log(e)
        this.$message.error('提交失败')
      }
    },
    onDialogClose () {
      this.submitData = {
        directoryName: '',
        id: '',
        subjectID: ''
      }
      this.$refs.submitDialog.clearValidate()
    },
    onEditClick (item) {
      this.submitData = item
      this.dialogShow = true
    }
  },
  computed: {
    statusList () {
      return statusList
    }
  },
  filters: {
    getButtyTime (timeSrc) {
      return moment(timeSrc).format('YYYY-MM-DD hh:mm:ss')
    }
  },
  created () {
    this.getDirectList()
    this.getSimpleSubjects()
  }
}
</script>

<style scoped lang='less'>
.container {
  margin: 10px;

  .box-card {
    .total-item {

    }
  }
}
</style>
